﻿// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System;
using Byond.EnsembleLibrary;
using Utils.DialogManagement;
using Utils.Messaging;

namespace Byond.Frontend
{
	class LibraryFactoryResetWorkflow
	{
		private readonly Library _library;
		private readonly IDialogManager _dialogManager;
		private readonly Env _env;

		public event MessageEventHandler SendMessage;

		public void ResetToFactoryDefaultsIfEmpty()
		{
			var rootFolder = _library.CreateRootFolder();
			if (!rootFolder.IsEmpty)
				return;

			var dialog = _dialogManager.CreateMessageDialog(
				"Do you want to create a default library in your Documents folder?",
				"No library was found",
				DialogMode.YesNo);
			dialog.Yes = ResetToFactoryDefaults;
			dialog.Show();
		}

		private void ResetToFactoryDefaults()
		{
			try
			{
				_library.ResetToFactoryDefaults(true);
			}
			catch (Exception e)
			{
				// TODO: UserLogger
				_dialogManager.CreateMessageDialog(
					"Could not reset the library to factory defaults.", 
					"Error",
					DialogMode.Ok);
			}
		}
	}
}
